package com.xxx.baseapplication.dao

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.xxx.baseapplication.vo.UrlTypeVO

@Database(
    entities = [UrlTypeVO::class],
    version = 1
)
abstract class AppDataBase : RoomDatabase() {

    abstract fun urlTypeDao(): UrlTypeDao

    companion object {
        private var instance: AppDataBase? = null

        fun init(context: Context) {
            instance = Room.databaseBuilder(
                context.applicationContext,
                AppDataBase::class.java,
                "database"
            )
                .addMigrations(MIGRATION_1_2)
                .allowMainThreadQueries()
                .build()
        }

        fun get(): AppDataBase {
            return instance!!
        }

        private val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(_db: SupportSQLiteDatabase) {
                _db.execSQL("CREATE TABLE IF NOT EXISTS `UrlTypeVO` (`urlMd5` TEXT NOT NULL, `type` TEXT NOT NULL, PRIMARY KEY(`urlMd5`))")
            }
        }
    }
}